#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include "SyncBase6RW.h"
#include "cmoduleinterface.h"

//库导出的模块对象
CModuleParent *getModulePtrEx()
{
    SyncBase6RW* ptr = new SyncBase6RW();
    return ptr;
}

SyncBase6RW::SyncBase6RW()
{
	m_pSyncBaseTh = new CSyncBaseThread();
}

SyncBase6RW::~SyncBase6RW()
{
	if (m_pSyncBaseTh)
	{
		m_pSyncBaseTh->ThreadStop();
		delete m_pSyncBaseTh;
		m_pSyncBaseTh = nullptr;
	}
}

void SyncBase6RW::setModuleMgr(CModuleMgrParent *pMgr)
{
    m_pModuleMgr = pMgr;
}

std::string SyncBase6RW::version()
{
	return "SyncBaseRW 1.0.0";
}

void SyncBase6RW::setConfig(const std::string &sCfg)
{
	// 解析 JSON 字符串
	QJsonDocument jsonDoc = QJsonDocument::fromJson(QString::fromStdString(sCfg).toUtf8());
	QString sIp;
	int nPort;
	QString sUserName;
	QString sPassWd;
	QString sDbName;
	if (jsonDoc.isObject())
	{
		QJsonObject jsonObj = jsonDoc.object();
		if (jsonObj.contains("sIp"))
		{
			sIp = jsonObj["sIp"].toString();
		}
		if (jsonObj.contains("nPort"))
		{
			nPort = jsonObj["nPort"].toInt();
		}
		if (jsonObj.contains("sUserName"))
		{
			sUserName = jsonObj["sUserName"].toString();
		}
		if (jsonObj.contains("sPassWd"))
		{
			sPassWd = jsonObj["sPassWd"].toString();
		}
		if (jsonObj.contains("sDbName"))
		{
			sDbName = jsonObj["sDbName"].toString();
		}
	}
	if (m_pSyncBaseTh)
	{
		m_pSyncBaseTh->config(sIp, nPort, sUserName, sPassWd, sDbName);
    }
}

std::string SyncBase6RW::config()
{
    return m_pSyncBaseTh->getConfig().toStdString();
}

int SyncBase6RW::start()
{
    if (m_pSyncBaseTh!=nullptr)
    {
        m_pSyncBaseTh->ThreadStart();
        m_pSyncBaseTh->startSyncBase();
        return 0;
    }
    return -1;
}

void SyncBase6RW::stop()
{
    if(m_pSyncBaseTh!=nullptr)
    {
        m_pSyncBaseTh->ThreadStop();
    }
}

int SyncBase6RW::enumTag(const std::string &addr, unsigned short *nCount)
{
	return -1;
}

int SyncBase6RW::checkFmt(const std::string &addr)
{
	return -1;
}

int SyncBase6RW::addTag(const std::string &addr, const int &nType, const unsigned int &cyc)
{
	if (m_pSyncBaseTh)
	{
		return m_pSyncBaseTh->addTag(QString::fromStdString(addr), nType, cyc);
	}
	return -1;
}

int SyncBase6RW::delTag(const std::string &addr)
{
	if (m_pSyncBaseTh)
	{
		return m_pSyncBaseTh->delTag(QString::fromStdString(addr));
	}
	return -1;
}

void SyncBase6RW::clear()
{
	if (m_pSyncBaseTh)
	{
		return m_pSyncBaseTh->clear();
	}
}

CTag* SyncBase6RW::value(const std::string &addr)
{
	if (m_pSyncBaseTh)
	{
		return m_pSyncBaseTh->value(QString::fromStdString(addr));
	}
	return nullptr;
}

VecPTag SyncBase6RW::values(const VecStr &vecAddr)
{
	VecPTag vecPTag;
	if (m_pSyncBaseTh)
	{
		vecPTag= m_pSyncBaseTh->values(vecAddr);
	}
	return vecPTag;
}

int SyncBase6RW::setValue(const std::string &addr, CTag *pTag)
{
	if (m_pSyncBaseTh)
	{
		return m_pSyncBaseTh->setValue(QString::fromStdString(addr), pTag);
	}
    return -1;
}

int SyncBase6RW::setValue(const std::string &addr, CTag &Tag)
{
    if (m_pSyncBaseTh)
    {
        return m_pSyncBaseTh->setValue(QString::fromStdString(addr), Tag);
    }
    return -1;
}

int SyncBase6RW::setValues(const VecStr &vecAddr, VecPTag vecTags)
{
	if (m_pSyncBaseTh)
	{
		return m_pSyncBaseTh->setValues(vecAddr, vecTags);
	}
    return -1;
}

int SyncBase6RW::setValueHistory(const std::string &addr, CTag *pTag)
{
    if (m_pSyncBaseTh)
    {
        return m_pSyncBaseTh->setValueHistory(QString::fromStdString(addr), pTag);
    }
    return -1;
}

int SyncBase6RW::setValueHistory(const std::string &addr, CTag &Tag)
{
    if (m_pSyncBaseTh)
    {
        return m_pSyncBaseTh->setValueHistory(QString::fromStdString(addr), Tag);
    }
    return -1;
}

int SyncBase6RW::setValuesHistory(const VecStr &vecAddr, VecPTag vecTags)
{
    if (m_pSyncBaseTh)
    {
        return m_pSyncBaseTh->setValuesHistory(vecAddr, vecTags);
    }
    return -1;
}

void SyncBase6RW::setLogRet(LogCallback callback)
{
    if (m_pSyncBaseTh)
    {
        return m_pSyncBaseTh->setLogRet(callback);
    }
}
